package com.degon.hsyl.anchorscrollview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;


import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private TabLayout mInside_tab;
    private TabLayout mOutside_tab;
    private TextView mAiming_point1;
    private TextView mAiming_point2;
    private TextView mAiming_point3;
    private TextView mAiming_point4;
    private TextView mAiming_point5;
    private TextView mAiming_point6;
    private String[] pointText = new String[]{"锚点1", "锚点2", "锚点3", "锚点4", "锚点5", "锚点6"};
    private ObservableScrollView mScrollView;
    private ArrayList<TextView> mAimingPointList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        /**创建锚点容器*/
        mAimingPointList = new ArrayList<>();
        mOutside_tab = (TabLayout) findViewById(R.id.outside_tab);
        mInside_tab = (TabLayout) findViewById(R.id.inside_tab);
        mAiming_point1 = (TextView) findViewById(R.id.aiming_point1);
        mAiming_point2 = (TextView) findViewById(R.id.aiming_point2);
        mAiming_point3 = (TextView) findViewById(R.id.aiming_point3);
        mAiming_point4 = (TextView) findViewById(R.id.aiming_point4);
        mAiming_point5 = (TextView) findViewById(R.id.aiming_point5);
        mAiming_point6 = (TextView) findViewById(R.id.aiming_point6);
        mScrollView = (ObservableScrollView) findViewById(R.id.sv_aiming_point);
        /**添加锚点*/
        mAimingPointList.add(mAiming_point1);
        mAimingPointList.add(mAiming_point2);
        mAimingPointList.add(mAiming_point3);
        mAimingPointList.add(mAiming_point4);
        mAimingPointList.add(mAiming_point5);
        mAimingPointList.add(mAiming_point6);
        /**初始化tabLayout*/
        for (int i = 0; i < pointText.length; i++) {
            addAimingPoint(mInside_tab, pointText[i]);
            addAimingPoint(mOutside_tab, pointText[i]);
        }
        new ScrollViewTheAnchor(mInside_tab, mOutside_tab, mScrollView, mAimingPointList, this, 50);


    }

    /**
     * 给TabLayout添加条目
     */
    public void addAimingPoint(TabLayout tabLayout, String text) {
        tabLayout.addTab(tabLayout.newTab().setText(text));
    }
}